package org.example.camundaengine.serviceTask;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 */
@Service("telCall")
@Slf4j
public class TelCallService {
    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    public Integer doCall(DelegateExecution execution) {
        log.info("当前流程实例-{}, 执行-{}, 事件名称-{}",
                execution.getProcessInstanceId(),
                execution.getCurrentActivityName(),
                execution.getEventName()
        );
        // 回退测试
        if (execution.getVariable("redo") != null && (Boolean) execution.getVariable("redo")) {
            log.info("重新打分：6分");
            execution.setVariable("rescore", 6);
            return (Integer) execution.getVariable("score");
        }
        /**
         * VariablesLocal 是该活动的变量，生命周期与活动一致。
         * Variables 是流程变量，生命周期与流程一致。当设置同名变量时，会进行覆盖。
         * 所有变量会：
         * 保存在表 act_ru_variable中，当有Task_ID时，代表是Local
         * act_hi_varinst 为历史变量表
         * */
        String repairManName = String.valueOf(execution.getVariable("repairManName"));
        log.info("请对-{}的服务打分", repairManName);
        return 10;
    }

    public void getScore(DelegateExecution execution) {
        log.info("当前流程实例-{}, 执行-{}, 事件名称-{}",
                execution.getProcessInstanceId(),
                execution.getCurrentActivityName(),
                execution.getEventName()
        );
        // 回退
        if (execution.getVariable("rescore") == null) {
            ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(execution.getProcessDefinitionId());
            List<ActivityImpl> activityList = processDefinitionEntity.getActivities();
            log.info("该流程的所有活动{}", activityList);
            int target = -1;
            int i = 0;
            for (; i < activityList.size(); i++, target++) {
                if (activityList.get(i).getActivityId() == execution.getCurrentActivityId()) {
                    break;
                }
            }
            if (target < 0) {
                target = 0;
            }
            log.info("当前节点位置:{}，id:{},name:{}, 回退节点位置:{}，id:{},name:{}"
                    , i, activityList.get(i).getActivityId(), activityList.get(i).getName()
                    , target, activityList.get(target).getActivityId(), activityList.get(target).getName());
            log.info("想要重新打分");
            // 修改当前流程至上一流程前
            runtimeService
                    .createProcessInstanceModification(execution.getProcessInstanceId())
                    .startBeforeActivity(activityList.get(target).getActivityId(), execution.getActivityInstanceId())
                    .setVariable("redo", true)
                    .cancelActivityInstance(execution.getActivityInstanceId())
                    .execute();
            return;
        }
        log.info("员工-{}的服务得分是：{}",
                execution.getVariable("repairManName"),
                execution.getVariable("score"));

    }

}
